package cn.teduymdj.jxc.service.impl;

import cn.teduymdj.jxc.ex.ServiceException;
import cn.teduymdj.jxc.mapper.GoodsMapper;
import cn.teduymdj.jxc.mapper.OverflowListGoodsMapper;
import cn.teduymdj.jxc.mapper.OverflowListMapper;
import cn.teduymdj.jxc.pojo.dto.*;
import cn.teduymdj.jxc.pojo.entity.OverflowList;
import cn.teduymdj.jxc.pojo.entity.OverflowListGoods;
import cn.teduymdj.jxc.pojo.vo.GoodsStandardVO;
import cn.teduymdj.jxc.pojo.vo.OverflowListGoodsListItemVO;
import cn.teduymdj.jxc.pojo.vo.OverflowListListItemVO;
import cn.teduymdj.jxc.security.LoginPrincipal;
import cn.teduymdj.jxc.service.OverflowListGoodsService;
import cn.teduymdj.jxc.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * 报溢业务实现
 * 欧阳林枫
 */
@Slf4j
@Service
@Transactional
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {
    @Autowired
    OverflowListGoodsMapper overflowListGoodsMapper;

    @Autowired
    OverflowListMapper overflowListMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public void save(OverflowListAddNewDTO02 overflowListAddNewDTO02) {
        log.debug("处理保存商品报溢单的业务:{}", overflowListAddNewDTO02);
        int overflowNumber = overflowListMapper.countByOverflowNumber(overflowListAddNewDTO02.getOverflowNumber());
        //查询单号是否存在
        if (overflowNumber != 0) {
            // 是:添加失败，抛出异常，单号已存在
            String message = "保存商品报溢单单信息失败，单号已存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        OverflowListAddNewDTO overflowListAddNewDTO = new OverflowListAddNewDTO();
        BeanUtils.copyProperties(overflowListAddNewDTO02,overflowListAddNewDTO);
        overflowListAddNewDTO.setUserId(getLoginInfo().getId());
        int result = overflowListMapper.insert(overflowListAddNewDTO);
        // 判断是否添加成功
        if (result != 1) {
            // 否:添加失败，抛出异常，请稍后重试
            String message = "添加失败，服务器忙，请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        List<OverflowListGoodsAddNewDTO> overflowListGoodsAddNewDTOList = overflowListAddNewDTO02.getTableData();
        if (overflowListGoodsAddNewDTOList==null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "保存报溢单信息失败,商品不存在!");
        }
        // 保存报溢单商品信息
        for (OverflowListGoodsAddNewDTO overflowListGoodsAddNewDTO : overflowListGoodsAddNewDTOList) {
            //判断单价乘数量和总金额是否相等
            BigDecimal price = overflowListGoodsAddNewDTO.getPrice();
            Integer num = overflowListGoodsAddNewDTO.getNum();
            BigDecimal number = new BigDecimal(num);
            BigDecimal total = overflowListGoodsAddNewDTO.getTotal();
            if (!price.multiply(number).equals(total)) {
                throw new ServiceException(ServiceCode.ERR_INSERT, "保存报溢单信息失败,插入金额有误!");
            }
            //通过id查询数据库信息是否存在
            Long goodsId = overflowListGoodsAddNewDTO.getGoodsId();
            GoodsStandardVO goods = goodsMapper.getStandardById(goodsId);
            if (goods == null) {
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "保存报溢单信息失败,商品不存在!");
            }
            //设置报溢单id
            Long id = overflowListAddNewDTO.getId();
            overflowListAddNewDTO.setId(id);
            // 修改商品上一次进货价，进货均价，库存，状态
            goods.setLastPurchasingPrice(overflowListGoodsAddNewDTO.getPrice());
            goods.setInventoryQuantity(goods.getInventoryQuantity());
            int t = 2;
            BigDecimal two = new BigDecimal(t);
            goods.setPurchasingPrice(goods.getLastPurchasingPrice().multiply(overflowListGoodsAddNewDTO.getPrice()).divide(two));
            goods.setState(2);
            GoodsAddNewDTO goodsAddNewDTO = new GoodsAddNewDTO();
            BeanUtils.copyProperties(goods, goodsAddNewDTO);
            goodsMapper.addNew(goodsAddNewDTO);

        }
        //插入进货商品实体表
        int rows = overflowListGoodsMapper.insertBatch(overflowListGoodsAddNewDTOList);
        if (rows < 1) {
            throw new ServiceException(ServiceCode.ERR_INSERT, "保存进货单信息失败,服务器忙,请稍后再试!");
        }


    }

    @Override
    public List<OverflowListListItemVO> getOverflowlist(SelectConditionDTO selectConditionDTO) {
        return overflowListMapper.listByTime(selectConditionDTO);
    }

    @Override
    public List<OverflowListGoodsListItemVO> getOverflowListGoodsByOverflowListId(Long overflowListId) {
        return overflowListGoodsMapper.listByListId(overflowListId);
    }

    @Override
    public OverflowList getOverflowList(Long overflowListId) {
        return null;
    }

    private LoginPrincipal getLoginInfo() {
        return (LoginPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }
}
